#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include"BlockQueue.hpp"
#include"Task.hpp"

void* Consumer(void* args)
{
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t>*>(args);
    while(true)
    {
        //sleep(2);
        //1.获取数据
        //int data = 0;
        //Task t;
        task_t t;
        bq->Pop(&t);
        //2.处理数据
        //t.Excute();
        t();
        //std::cout << "Consumer -> " << t.result() << std::endl;
    }
}
void* Productor(void* args)
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t>*>(args);
    while(true)
    {
        //1.构建数据
        //int data = rand() % 10 + 1;
        // int x = rand()%10 + 1;
        // usleep(x * 1000);
        // int y = rand()%10 + 1;
        //Task t(x, y);
        //2.生产数据
        bq->Equeue(Download);
        //std::cout << "Productor -> " << t.debug() << std::endl;
        std::cout << "Productor -> Download" << std::endl;
        sleep(1);

    }
}
int main()
{
    BlockQueue<task_t> *bq = new BlockQueue<task_t>();
    pthread_t c1, c2, c3, p1, p2;
    pthread_create(&c1, nullptr, Consumer, bq);
    pthread_create(&c2, nullptr, Consumer, bq);
    pthread_create(&c3, nullptr, Consumer, bq);

    pthread_create(&p1, nullptr, Productor, bq);
    pthread_create(&p2, nullptr, Productor, bq);


    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);

    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);

    return 0;
}